package com.yhy.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.util.Factory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

public class HelloWorld {
	public static void main(String[] args) {
		// 读取配置文件，初始化 SecurityManager
		Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:jdbc_realm.ini");
		// 获取 SecurityManager 实例
		SecurityManager securityManager = factory.getInstance();
		// 把 SecurityManager 绑定到 SecurityUtils
		SecurityUtils.setSecurityManager(securityManager);
		// 得到当前执行的用户
		Subject curUser = SecurityUtils.getSubject();
		// 创建 token 令牌，用户名，密码
		UsernamePasswordToken token = new UsernamePasswordToken("java1234", "123456");
		try {
			// 身份验证
			curUser.login(token);
			System.out.println("身份认证成功！");
			//退出
			curUser.logout();
		} catch (AuthenticationException e) {
			System.out.println("身份认证失败！");
		} finally {
		}
	}
}
